package org.geekbang.thinking.in.spring.ioc.bean.lifecycle;

import com.think.in.spring.ioc.overview.domain.User;
import org.springframework.beans.factory.*;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;

import javax.annotation.PostConstruct;

/**
 * @author zjiajia
 * @date 2022/2/27 9:11
 */
public class UserHolder implements BeanNameAware, BeanClassLoaderAware, BeanFactoryAware, EnvironmentAware
, InitializingBean {

    private final User user;

    private Integer number;

    private String description;


    private ClassLoader classLoader;


    private BeanFactory beanFactory;

    private String beanName;

    private Environment environment;

    public BeanFactory getBeanFactory() {
        return beanFactory;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) {
        this.beanFactory = beanFactory;
    }

    public String getBeanName() {
        return beanName;
    }

    @Override
    public void setBeanName(String beanName) {
        this.beanName = beanName;
    }

    public Environment getEnvironment() {
        return environment;
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    public UserHolder(User user) {
        this.user = user;
    }

    public User getUser() {
        return user;
    }

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "UserHolder{" +
                "user=" + user +
                ", number=" + number +
                ", description='" + description + '\'' +
                '}';
    }

    @PostConstruct
    public void initPostConstruct(){
        this.description = "test v4";
        System.out.println("initPostConstruct ---- v4");
    }

    public void init(){
        this.description = "test v6";
        System.out.println("initPostConstruct ---- v4");
    }

    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        this.classLoader = classLoader;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        this.description = "test v5";
        System.out.println("initPostConstruct ---- v4");
    }
}
